import { APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core'
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'
import { AppController } from '@app/app.controller'

import { ValidationPipe } from '@app/pipes/validation.pipe'

// 拦截器
import { HttpCacheInterceptor } from '@app/interceptors/cache.interceptor'

// 中间件
import { CorsMiddleware } from '@app/middlewares/cors.middleware'
import { OriginMiddleware } from '@app/middlewares/origin.middleware'

// 公共模块
import { DatabaseModule } from '@app/processors/database/database.module'
import { CacheModule } from '@app/processors/cache/cache.module'
import { HelperModule } from '@app/processors/helper/helper.module'

// 业务模块（核心）
import { AuthModule } from '@app/modules/auth/auth.module'
import { OptionModule } from '@app/modules/option/option.module'
import { AnnouncementModule } from '@app/modules/announcement/announcement.module'
import { FeedbackModule } from './modules/feedback/feedback.module';
import { UserModule } from './modules/user/user.module';
import { ArticleModule } from './modules/article/article.module'
import { ArchiveModule } from './modules/archive/archive.module'
import { CategoryModule } from './modules/category/category.module'
import { TagModule } from './modules/tag/tag.module'
import { VoteModule } from './modules/vote/vote.module'
import { AiModule } from './modules/ai/ai.module';
import { MapModule } from './modules/map/map.module';
import { ItineraryModule } from './modules/itinerary/itinerary.module';

@Module({
  imports: [
    HelperModule,
    DatabaseModule,
    CacheModule,

    AuthModule,
    OptionModule,
    AnnouncementModule,
    FeedbackModule,
    UserModule,
    ArchiveModule,
    ArticleModule,
    CategoryModule,
    TagModule,
    VoteModule,
    AiModule,
    MapModule,
    ItineraryModule,
  ],
  controllers: [AppController],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: HttpCacheInterceptor,
    },
    {
      provide: APP_PIPE,
      useClass: ValidationPipe,
    },
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(CorsMiddleware, OriginMiddleware).forRoutes('*')
  }
}
